{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 文件操作\n",
    "- 打开文件\n",
    "- 文件关闭\n",
    "- 文件写入\n",
    "- 读取文件"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "###  打开文件\n",
    "使用open方法打开文件，返回文件对象\n",
    "- open(file,mode='r',enconding='')\n",
    "- file 打开文件路径\n",
    "- mode打开方式  r读  w覆盖写  a追加写  b二进制模式  t文本模式（默认）\n",
    "- encoding  一般使用utf8\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "###  文件关闭\n",
    "- close()方法用于文件关闭"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "###  文件写入\n",
    "- write()方法向指定文件写入\n",
    "- writeline()方法可以写入一序列字符串，一序列可以是可迭代对象"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "23\n"
     ]
    }
   ],
   "source": [
    "file = open('test.py','w')\n",
    "length = file.write('print(\"hello world123\")')# write方法会返回写入字符的个数\n",
    "print(length)\n",
    "file.close()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "17\n"
     ]
    }
   ],
   "source": [
    "file = open('index.html','w',encoding='utf8')\n",
    "length = file.write('<h1>这是个html文件<h1>') # write方法会返回写入字符的个数\n",
    "print(length)\n",
    "file.close()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "outputs": [],
   "source": [
    "file = open('test.py','w')\n",
    "length = file.writelines(['123\\n','456\\n','789'])\n",
    "\n",
    "file.close()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "###  读取文件\n",
    "- read(字节数)方法参数为读取字节数\n",
    "- readline()方法从文件读取整行，包括\\n字符如果指定一个非负参数则返回指定大小的字节数包括\\n字符\n",
    "- readlines()读取所有行，返回一个列表"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "aaaaaa\n",
      "bbbbbb\n",
      "cccccc\n",
      "dddddd\n"
     ]
    }
   ],
   "source": [
    "file = open('abc.txt','r')\n",
    "print(file.read(100))\n",
    "file.close()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "aaaa\n",
      "aa\n",
      "\n"
     ]
    }
   ],
   "source": [
    "file = open('abc.txt','r')\n",
    "print(file.readline(4))\n",
    "print(file.readline(5))\n",
    "file.close()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['aaaaaa\\n', 'bbbbbb\\n', 'cccccc\\n', 'dddddd']\n"
     ]
    }
   ],
   "source": [
    "file = open('abc.txt','r')\n",
    "print(file.readlines())\n",
    "\n",
    "file.close()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}